package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Enzo Cotter on 2020/10/3.
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    StorageMapper storageMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    LogMapper logMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    PermissionMapper_empower permissionMapperEmpower;


    @Override
    public ListBean queryAdminListBean(String username, Integer page, Integer limit, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);

        //example 提供条件
        AdminExample adminExample = new AdminExample();
        //and + 列名 + 条件
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if(username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        adminExample.setOrderByClause(sort + " " + order);
        List<Admin> admins = adminMapper.selectByExample(adminExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(admins);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setItems(admins);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public Integer addAdmin(Admin admin) {
        int insert = adminMapper.insert(admin);
        return insert;
    }

    @Override
    public ListBean queryStorage(String name, String key,Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        StorageExample storageExample = new StorageExample();
        //and + 列名 + 条件
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if(name != null){
            criteria.andNameLike("%" + name + "%");
        }
        if(key != null){
            criteria.andKeyEqualTo(key);
        }
        storageExample.setOrderByClause(sort + " " + order);
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(storages);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setItems(storages);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public int addStorage(Storage storage) {
        int insert = storageMapper.insert(storage);
        return insert;
    }

    @Override
    public int updateStorageName(Integer id, String name, Date date) {
        int i = storageMapper.updateByName(id,name,date);
        return i;
    }

    @Override
    public int deleteStorageName(Integer id) {
        int i = storageMapper.deleteById(id);
        return i;
    }

    @Override
    public List<RoleList> queryRole() {
        List<RoleList> lists = roleMapper.selectRole();
        return lists;
    }

    @Override
    public int updateAdmin(Admin admin,Date date) {
        int i = adminMapper.updateByAdmin(admin,date);
        return i;
    }

    @Override
    public int deleteAdmin(Integer id) {
        int i = adminMapper.deleteById(id);
        return i;
    }

    @Override
    public ListBean queryLogListBean(String name, Integer page, Integer limit, String sort, String order) {

        PageHelper.startPage(page,limit);
        LogExample logExample = new LogExample();
        //and + 列名 + 条件
        LogExample.Criteria criteria = logExample.createCriteria();
        if(name != null){
            criteria.andAdminLike("%" + name + "%");
        }
        logExample.setOrderByClause(sort + " " + order);
        List<Log> list = logMapper.selectByExample(logExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setItems(list);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public ListBean queryRoleByInfo(String name, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        RoleExample roleExample = new RoleExample();
        //and + 列名 + 条件
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if(name != null){
            criteria.andNameLike("%" + name + "%");
        }
        roleExample.setOrderByClause(sort + " " + order);
        List<Role> roles = roleMapper.selectByExample(roleExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(roles);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setItems(roles);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public int addRoleByName(Role role) {
        Date date = new Date();
        int i = roleMapper.insertByName(role,date);
        return i;
    }

    @Override
    public int updateRole(Role role, Date date) {
        int i = roleMapper.updateByRole(role,date);
        return i;
    }

    @Override
    public int deleteRole(Integer id) {
        int i = roleMapper.deleteByRoleId(id);
        return i;
    }

    @Override
    public List<Admin> queryFiledByUsername(String username,String fileds) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if(username != null){
            criteria.andUsernameLike("%" + username + "%");
        }

        List<Admin> results = adminMapper.selectCondition(adminExample, fileds);
        return results;
    }

    @Override
    public List<String> selectPermissionByUsername(String username) {
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andUsernameEqualTo(username);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Integer[] roleIds = admins.get(0).getRoleIds();


        ArrayList<String> peimissionList = new ArrayList<String>();
        for (Integer roleId:roleIds){

            PermissionExample permissionExample = new PermissionExample();
            permissionExample.createCriteria().andRoleIdEqualTo(roleId);
            List<Permission> permissions = permissionMapper.selectByExample(permissionExample);
            for (Permission permission:permissions){
                peimissionList.add(permission.getPermission());
            }
        }
        return peimissionList;
    }

    @Override
    public Integer updatePermissionsByRoleId(PermissionUpdateBean_empower permissionUpdateBeanEmpower) {
        String roleId = permissionUpdateBeanEmpower.getRoleId();
        //先删掉原来该角色对应的权限
        PermissionExample_empower example = new PermissionExample_empower();
        example.createCriteria().andRoleIdEqualTo(roleId);
        permissionMapperEmpower.deleteByExample(example);
        //再把该角色刚刚获得的权限插入permission数据库
        List<String> permissions = permissionUpdateBeanEmpower.getPermissions();
        return permissionMapperEmpower.insertPermissionsByRoleId(permissions, roleId);
    }

    @Override
    public Map<String, Object> getPermissions(String roleId) {
        Map<String, Object> map = new HashMap<>();
        if(roleId.equals("1")){
            List<String> assignedPermissions = getAllPermissionsByRoleId(roleId);
            PermissionsSystem_empower systemPermissions = selectInfo();
            map.put("systemPermissions", systemPermissions.getSystemPermissions());
            map.put("assignedPermissions", assignedPermissions);
            return map;
        }else {
            List<String> assignedPermissions = getAllPermissionsByRoleId(roleId);
            PermissionsSystem_empower systemPermissions = selectAllInfo(roleId);
            map.put("systemPermissions", systemPermissions.getSystemPermissions());
            map.put("assignedPermissions", assignedPermissions);
            return map;
        }
    }

    private PermissionsSystem_empower selectInfo() {
        List<PermissionsSystem1_empower> sysPermissionList = selectAllListInfo();
        PermissionsSystem_empower permissionsSystemEmpower = new PermissionsSystem_empower();
        permissionsSystemEmpower.setSystemPermissions(sysPermissionList);
        return permissionsSystemEmpower;
    }

    private List<PermissionsSystem1_empower> selectAllListInfo() {
        List<PermissionApi_empower> list3 = roleMapper.selectPermissionApiAll();
        List<PermissionsSystem2_empower> list2 = roleMapper.selectPermissionsSystem2All();
        for (PermissionsSystem2_empower permissionsSystem2Empower : list2) {
            permissionsSystem2Empower.setChildren(list3);
        }
        List<PermissionsSystem1_empower> list1 = roleMapper.selectPermissionsSystem1All();
        for (PermissionsSystem1_empower permissionsSystem1Empower : list1) {
            permissionsSystem1Empower.setChildren(list2);
        }
        return list1;
    }

    private PermissionsSystem_empower selectAllInfo(String roleId) {
        List<PermissionsSystem1_empower> sysPermissionList = selectList(roleId);
        PermissionsSystem_empower permissionsSystemEmpower = new PermissionsSystem_empower();
        permissionsSystemEmpower.setSystemPermissions(sysPermissionList);
        return permissionsSystemEmpower;
    }

    private List<PermissionsSystem1_empower> selectList(String roleId) {
        List<PermissionApi_empower> list3 = roleMapper.selectPermissionApi(roleId);
        List<PermissionsSystem2_empower> list2 = roleMapper.selectPermissionsSystem2(roleId);
        for (PermissionsSystem2_empower permissionsSystem2Empower : list2) {
            permissionsSystem2Empower.setChildren(list3);
        }
        List<PermissionsSystem1_empower> list1 = roleMapper.selectPermissionsSystem1(roleId);
        for (PermissionsSystem1_empower permissionsSystem1Empower : list1) {
            permissionsSystem1Empower.setChildren(list2);
        }
        return list1;
    }

    /**
     * 获取所有权限列表
     *
     * @param roleId
     * @return
     */
    private List<String> getAllPermissionsByRoleId(String roleId) {
        if (roleId.equals("1")) return permissionMapperEmpower.selectAllPermission();
        return permissionMapperEmpower.selectPermissionList(roleId);
    }

}
